/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.application.PageSecurityException;
import com.jb.web.bean.HtmlItem;
import com.jb.web.bean.Region;
import com.jb.web.context.WebContext;

import com.jb.faces.component.UIComponent;
import com.jb.faces.context.FacesContext;


//
//this renderer will render a page into its space, can be used as 
//a component editor, where a form page can render into this
//
public class ComponentViewRenderer extends DefaultRenderer {
    public void decode(FacesContext pCtx, UIComponent item) {
        HtmlItem hItem = (HtmlItem) item;
        String value = (String) evaluate((WebContext) pCtx, hItem, hItem.getValue());

        if (!isEmptyString(value)) {
            hItem.setDecodedValue(value);
        }
    }

    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer("");
        Region sect = (Region) itemToRender;
        String value = (String) sect.getDecodedValue();
        HtmlItem itemToView = null;

        if (!isEmptyString(value)) {
            if (value.endsWith(".xsp")) {
                value = value.substring(0, value.length() - 4);
            }

            if (!value.equals(pCtx.getRespondTree().getId())) {
                try {
                    itemToView = (HtmlItem) pCtx.getSession().getPage(pCtx, value, false);
                } catch (PageSecurityException e) {
                    return html;
                }
            }
        }

        StringBuffer sectFormat = new StringBuffer(getPopupMenuAction(pCtx, sect)).append(getEditScript(pCtx, sect))
                                                                                  .append(addTagProperty(
                    "cellSpacing", "0", null)).append(addTagProperty("cellPadding", "0", null)).append(addTagProperty(
                    "style", sect.getDimensionStyle(), null));

        StringBuffer cellFormat = new StringBuffer().append(addTagProperty(
                    "style", sect.getBackgroundStyle() + sect.getBorderStyle(), null));

        html.append("<table ").append(sectFormat).append("><tr><td ").append(cellFormat).append(">");

        if (itemToView != null) {
            html.append(itemToView.render(pCtx));
        }

        html.append("</td></tr></table>");

        return html;
    }
}
